package com.urerp.web.common;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class SessionUtil {

	private static ServletRequestAttributes sra = null;
	private static HttpSession session = null;
	
	public static boolean getSession() {
		
		sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		session = sra.getRequest().getSession();
		
	    String sessionFlag = (String) session.getAttribute("sessionFlag");

	    if (sessionFlag != null && sessionFlag.equals("Y")) {
	    	return true;
	    }
		
		return false;
	}
	
	public static String getMemberSession(String key) {
		
		String value = "";
		
		if (getSession()) {
			sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
			session = sra.getRequest().getSession();

			@SuppressWarnings("unchecked")
			HashMap<String, String> memberMap = (HashMap<String, String>) session.getAttribute("memberSession");
			
			value = memberMap.get(key);
		}
		
				
		return value;
	}
	
	public static void setSession(String key, Object o) {
		
		sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		session = sra.getRequest().getSession();
		
		session.setAttribute(key, o);
		
	}
	
	public static void invalidate() {
		
		sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		session = sra.getRequest().getSession();
		
		session.invalidate();
	}
	
}
